import http from './request.js';

// 获取分类列表
export const getCategories = async (params = {}) => {
  try {
    const response = await http.get('/categories', { params });
    return response;
  } catch (error) {
    console.error('获取分类列表失败:', error);
    return {
      code: 500,
      message: '获取分类列表失败',
      data: []
    };
  }
};

// 获取标签列表
export const getTags = async (params = {}) => {
  try {
    const response = await http.get('/tags', { params });
    return response;
  } catch (error) {
    console.error('获取标签列表失败:', error);
    return {
      code: 500,
      message: '获取标签列表失败',
      data: []
    };
  }
};

// 获取文章列表
export const getArticles = async ({ category, tag, page = 1, pageSize = 10 }) => {
  try {
    const params = {
      page,
      pageSize
    };
    
    if (category) params.category = category;
    if (tag) params.tag = tag;
    
    const response = await http.get('/articles', { params });
    return response;
  } catch (error) {
    console.error('获取文章列表失败:', error);
    return {
      code: 500,
      message: '获取文章列表失败',
      data: {
        list: [],
        total: 0,
        page: 1,
        pageSize: 10
      }
    };
  }
};

// 获取文章详情
export const getArticleDetail = async (id) => {
  try {
    const response = await http.get(`/articles/${id}`);
    return response;
  } catch (error) {
    console.error('获取文章详情失败:', error);
    if (error.response?.status === 404) {
      return {
        code: 404,
        message: '文章不存在',
        data: null
      };
    }
    return {
      code: 500,
      message: '获取文章详情失败',
      data: null
    };
  }
};

// 获取个人信息
export const getPersonalInfo = async () => {
  try {
    const response = await http.get('/personal-info');
    return response;
  } catch (error) {
    console.error('获取个人信息失败:', error);
    return {
      code: 500,
      message: '获取个人信息失败',
      data: {
        name: '博主',
        title: '前端开发工程师',
        description: '',
        initials: 'GB',
        about: [],
        skills: [],
        contact: []
      }
    };
  }
};

// 获取备案信息
export const getRecordInfo = async () => {
  try {
    const response = await http.get('/record-info');
    return response;
  } catch (error) {
    console.error('获取备案信息失败:', error);
    return {
      code: 500,
      message: '获取备案信息失败',
      data: {
        icp: '',
        beian: '',
        company: '',
        showIcp: false,
        showBeian: false,
        showCompany: false
      }
    };
  }
};